//
//  FTOptionPicker.m
//  FTPicker
//
//  Created by JackieYip on 2017/6/9.
//  Copyright © 2017年 mondial-d. All rights reserved.
//

#import "FTOptionPicker.h"

@interface FTOptionPicker () <FTPresenterDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, weak) id optionPickerDelegate;
@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSInteger itemCount;
@property (nonatomic) NSInteger selectedIndex;
@end

@implementation FTOptionPicker
@dynamic delegate;

- (instancetype)init
{
    if (self = [super init]) {
        self.itemTextAlignment = NSTextAlignmentCenter;
        self.itemHeight = 44.0f;
        self.selectedIndex = NSNotFound;
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
    }
    
    return self;
}

- (id<FTOptionPickerDelegate>)delegate
{
    return self.optionPickerDelegate;
}

- (void)setDelegate:(id<FTOptionPickerDelegate>)delegate
{
    super.delegate = self;
    self.optionPickerDelegate = delegate;
}

#pragma mark - Custom Properties

- (void)setItemTextAlignment:(NSTextAlignment)itemTextAlignment
{
    _itemTextAlignment = itemTextAlignment;
    for (UITableViewCell *cell in self.tableView.visibleCells)
        cell.textLabel.textAlignment = itemTextAlignment;
}

- (void)setItemTextColor:(UIColor *)itemTextColor
{
    _itemTextColor = itemTextColor;
    for (UITableViewCell *cell in self.tableView.visibleCells)
        cell.textLabel.textColor = itemTextColor;
}

- (void)setItemFont:(UIFont *)itemFont
{
    _itemFont = itemFont;
    for (UITableViewCell *cell in self.tableView.visibleCells)
        cell.textLabel.font = itemFont;
}

- (void)setItemHeight:(CGFloat)itemHeight
{
    _itemHeight = itemHeight;
    [self.tableView reloadData];
}

#pragma mark - FTPresenterDelegate methods

- (NSString *)titleForPresenter:(FTPresenter *)presenter
{
    if ([self.optionPickerDelegate respondsToSelector:@selector(titleForPresenter:)])
        return [self.optionPickerDelegate titleForPresenter:self];
    
    return nil;
}

- (UIView *)contentViewInPresenter:(FTPresenter *)presenter
{
    //    if ([self.optionPickerDelegate respondsToSelector:@selector(contentViewInPresenter:)])
    //        return [self.optionPickerDelegate contentViewInPresenter:self];
    
    self.itemCount = [self.optionPickerDelegate numberOfItemsInPicker:self];
    CGFloat height = self.itemHeight*self.itemCount;
    if (height > self.maxContentHeight)
        height = floorf(self.maxContentHeight/self.itemHeight) * self.itemHeight;
    
    CGRect rect = self.tableView.frame;
    rect.size.height = height;
    self.tableView.frame = rect;
    
    [self.tableView reloadData];
    if (self.itemCount > 0)
        [self.tableView scrollRectToVisible:CGRectMake(0.0f, 0.0f, 1.0f, 1.0f) animated:NO];
    
    return self.tableView;
}

- (void)presenterWillDismiss:(FTPresenter *)presenter
{
    if ([self.optionPickerDelegate respondsToSelector:@selector(presenterWillDismiss:)])
        [self.optionPickerDelegate presenterWillDismiss:self];
}

- (void)presenterDidDismiss:(FTPresenter *)presenter
{
    if ([self.optionPickerDelegate respondsToSelector:@selector(presenterDidDismiss:)])
        [self.optionPickerDelegate presenterDidDismiss:self];
    
    if ([self.optionPickerDelegate respondsToSelector:@selector(picker:didDismissWithItemSelectedAtIndex:)])
        [self.optionPickerDelegate picker:self didDismissWithItemSelectedAtIndex:self.selectedIndex];
    
    self.selectedIndex = NSNotFound;
}

#pragma mark - UITableViewDataSource & UItableViewDelegate methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.itemCount;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.itemHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseIdentifier = @"OptionPickerCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    
    cell.textLabel.textAlignment = self.itemTextAlignment;
    cell.textLabel.font = self.itemFont;
    cell.textLabel.textColor = self.itemTextColor;
    
    if (indexPath.row < self.itemCount) {
        if ([self.optionPickerDelegate respondsToSelector:@selector(picker:textForItemAtIndex:)])
            cell.textLabel.text = [self.optionPickerDelegate picker:self textForItemAtIndex:indexPath.row];
        
        if ([self.optionPickerDelegate respondsToSelector:@selector(picker:itemCheckedAtIndex:)])
            cell.accessoryType = [self.optionPickerDelegate picker:self itemCheckedAtIndex:indexPath.row] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([self.optionPickerDelegate respondsToSelector:@selector(picker:didSelectItemAtIndex:)])
        [self.optionPickerDelegate picker:self didSelectItemAtIndex:indexPath.row];
    
    self.selectedIndex = indexPath.row;
    
    [self dismiss];
}

@end
